﻿using System;
using System.Management;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
namespace XBoshi.SecurityCommon
{
    /// <summary> 
    /// register 的摘要说明. 
    /// 注册软件所用到的类 
    /// </summary> 
    public class Register
    {
        public String ApplicationPath { set; get; }
        private String ApllicationDriver { set; get; }

        private String GetApplicationDriver() {
            if (String.IsNullOrWhiteSpace(ApplicationPath))
            {
                return "C:";
            }
            else {
                try
                {
                    var path = ApplicationPath.Substring(0,2).ToUpper();
                    return path;
                }
                catch { }
            }
            return "C:";
        }


        #region 获取cpu序列号 硬盘ID 网卡硬地址
        /// <summary> 
        /// 获取cpu序列号 
        /// </summary> 
        /// <returns>string </returns> 
        public string GetCpuInfo()
        {
            string cpuInfo = "";
            ManagementClass cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
            }
            cpuInfo = cpuInfo.PadLeft(16, '0');
            return cpuInfo.Substring(0, 16);
        }
        /// <summary> 
        /// 获取硬盘ID 
        /// </summary> 
        /// <returns>string </returns> 
        public string GetHDid()
        {
            string HDid = "";
            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();
            foreach (ManagementObject mo in moc1)
            {
                HDid = (string)mo.Properties["Model"].Value;
            }
            HDid = HDid.PadLeft(16, '0');
            return HDid.Substring(0, 16);
        }

        /// <summary>
        /// 获取磁盘分配大小,基于16进制16位长，左补零
        /// </summary>
        /// <returns></returns>
        public string GetHDSize()
        {

            ApllicationDriver = GetApplicationDriver();
            String diskSize = "00";
            ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
            ManagementObjectCollection disks = diskClass.GetInstances();
            foreach (ManagementObject disk in disks)
            {
                if ((String)disk["DeviceID"] == ApllicationDriver)
                {
                    long size = Int64.Parse( disk["Size"].ToString()) ;
                    diskSize = String.Format("{0:X}", size);
                    break;
                }
            }
            diskClass.Dispose();
            diskSize=diskSize.PadLeft(16,'0');
            return diskSize.Substring(0,16);
        }



        /// <summary> 
        /// 获取网卡硬件地址 
        /// </summary> 
        /// <returns>string </returns> 
        public string GetMoAddress()
        {


            string MoAddress = "";
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc2 = mc.GetInstances();
            foreach (ManagementObject mo in moc2)
            {
                try
                {
                    if ((bool)mo["IPEnabled"] == true && true)
                    {
                        var netAddress = mo["MacAddress"].ToString();
                        var bb = netAddress.Split(':');
                        MoAddress = MoAddress + string.Join("", bb);
                        //break;
                        //Array  aa = (Array )mo["IPAddress"];
                        //Debug.WriteLine(MoAddress);
                        Debug.WriteLine(MoAddress);
                    }

                    mo.Dispose();
                }
                catch { 
                
                }
            }

            MoAddress = MoAddress.PadLeft(16, '0');
           return MoAddress.Substring(0, 16);
        }
        #endregion
    }
}